import*as e from"../../core/common/common.js";import*as t from"../../core/host/host.js";import*as i from"../../core/i18n/i18n.js";import*as o from"../../core/root/root.js";import{formatCSSChangesFromDiff as s}from"../utils/utils.js";import*as r from"../../third_party/diff/diff.js";import*as n from"../../ui/components/diff_view/diff_view.js";import*as a from"../../ui/legacy/legacy.js";import*as c from"../../models/workspace_diff/workspace_diff.js";import*as l from"../../core/platform/platform.js";import*as d from"../../models/workspace/workspace.js";import*as h from"../../ui/components/icon_button/icon_button.js";import*as f from"../snippets/snippets.js";const u=new CSSStyleSheet;u.replaceSync('[slot="insertion-point-main"]{flex-direction:column;display:flex}[slot="insertion-point-sidebar"]{overflow:auto}.diff-container{flex:1;overflow:auto}:focus.selected{--override-selected-color:#fff;background-color:var(--legacy-selection-bg-color);color:var(--override-selected-color)}.-theme-with-dark-background :focus.selected,\n:host-context(.-theme-with-dark-background) :focus.selected{--override-selected-color:rgb(0 0 0)}.changes-toolbar{background-color:var(--color-background-elevation-1);border-top:var(--legacy-divider-border)}\n/*# sourceURL=changesView.css */\n');const p=new CSSStyleSheet;p.replaceSync(".tree-outline li{min-height:20px}.tree-outline li:hover:not(.selected) .selection{display:block;--icon-color:var(--icon-default-hover)}.navigator-sm-script-tree-item,\n.navigator-script-tree-item,\n.navigator-snippet-tree-item{--icon-color:var(--icon-file-script)}.navigator-sm-stylesheet-tree-item,\n.navigator-stylesheet-tree-item{--icon-color:var(--icon-file-styles)}.navigator-image-tree-item{--icon-color:var(--icon-file-image)}.navigator-font-tree-item{--icon-color:var(--icon-file-font)}.force-white-icons{--icon-color:var(--icon-force-white)}@media (forced-colors: active){li,\n  .navigator-sm-script-tree-item,\n  .navigator-script-tree-item,\n  .navigator-snippet-tree-item,\n  .navigator-sm-stylesheet-tree-item,\n  .navigator-stylesheet-tree-item,\n  .navigator-image-tree-item,\n  .navigator-font-tree-item{forced-color-adjust:none;--icon-color:ButtonText}}\n/*# sourceURL=changesSidebar.css */\n");const m={sFromSourceMap:"{PH1} (from source map)"},g=i.i18n.registerUIStrings("panels/changes/ChangesSidebar.ts",m),S=i.i18n.getLocalizedString.bind(void 0,g);class C extends(e.ObjectWrapper.eventMixin(a.Widget.Widget)){treeoutline;treeElements;workspaceDiff;constructor(e){super(),this.treeoutline=new a.TreeOutline.TreeOutlineInShadow,this.treeoutline.setFocusable(!1),this.treeoutline.setComparator(((e,t)=>l.StringUtilities.compare(e.titleAsText(),t.titleAsText()))),this.treeoutline.addEventListener(a.TreeOutline.Events.ElementSelected,this.selectionChanged,this),a.ARIAUtils.markAsTablist(this.treeoutline.contentElement),this.element.appendChild(this.treeoutline.element),this.treeElements=new Map,this.workspaceDiff=e,this.workspaceDiff.modifiedUISourceCodes().forEach(this.addUISourceCode.bind(this)),this.workspaceDiff.addEventListener("ModifiedStatusChanged",this.uiSourceCodeMofiedStatusChanged,this)}selectUISourceCode(e,t){const i=this.treeElements.get(e);i&&i.select(t)}selectedUISourceCode(){return this.treeoutline.selectedTreeElement?this.treeoutline.selectedTreeElement.uiSourceCode:null}selectionChanged(){this.dispatchEventToListeners("SelectedUISourceCodeChanged")}uiSourceCodeMofiedStatusChanged(e){e.data.isModified?this.addUISourceCode(e.data.uiSourceCode):this.removeUISourceCode(e.data.uiSourceCode)}removeUISourceCode(e){const t=this.treeElements.get(e);if(this.treeElements.delete(e),this.treeoutline.selectedTreeElement===t){const e=t.previousSibling||t.nextSibling;e?e.select(!0):(t.deselect(),this.selectionChanged())}t&&(this.treeoutline.removeChild(t),t.dispose()),0===this.treeoutline.rootElement().childCount()&&this.treeoutline.setFocusable(!1)}addUISourceCode(e){const t=new b(e);this.treeElements.set(e,t),this.treeoutline.setFocusable(!0),this.treeoutline.appendChild(t),this.treeoutline.selectedTreeElement||t.select(!0)}wasShown(){super.wasShown(),this.treeoutline.registerCSSFiles([p])}}class b extends a.TreeOutline.TreeElement{uiSourceCode;eventListeners;constructor(e){super(),this.uiSourceCode=e,this.listItemElement.classList.add("navigator-"+e.contentType().name()+"-tree-item"),a.ARIAUtils.markAsTab(this.listItemElement);let t="document";f.ScriptSnippetFileSystem.isSnippetsUISourceCode(this.uiSourceCode)&&(t="snippet");const i=new h.Icon.Icon;i.data={iconName:t,color:"var(--icon-file-default)",width:"20px",height:"20px"},this.setLeadingIcons([i]),this.eventListeners=[e.addEventListener(d.UISourceCode.Events.TitleChanged,this.updateTitle,this),e.addEventListener(d.UISourceCode.Events.WorkingCopyChanged,this.updateTitle,this),e.addEventListener(d.UISourceCode.Events.WorkingCopyCommitted,this.updateTitle,this)],this.updateTitle()}updateTitle(){let e=this.uiSourceCode.displayName();this.uiSourceCode.isDirty()&&(e="*"+e),this.title=e;let t=this.uiSourceCode.url();this.uiSourceCode.contentType().isFromSourceMap()&&(t=S(m.sFromSourceMap,{PH1:this.uiSourceCode.displayName()})),this.tooltip=t}dispose(){e.EventTarget.removeEventListeners(this.eventListeners)}}var v=Object.freeze({__proto__:null,ChangesSidebar:C,UISourceCodeTreeElement:b});const w={revertAllChangesToCurrentFile:"Revert all changes to current file",copyAllChangesFromCurrentFile:"Copy all changes from current file",noChanges:"No changes",binaryData:"Binary data",sInsertions:"{n, plural, =1 {# insertion (+)} other {# insertions (+)}}",sDeletions:"{n, plural, =1 {# deletion (-)} other {# deletions (-)}}",copy:"Copy"},y=i.i18n.registerUIStrings("panels/changes/ChangesView.ts",w),I=i.i18n.getLocalizedString.bind(void 0,y);let T,E;class D extends a.Widget.VBox{emptyWidget;workspaceDiff;changesSidebar;selectedUISourceCode;#e;diffContainer;toolbar;diffStats;diffView;copyButton;copyButtonSeparator;constructor(){super(!0);const e=new a.SplitWidget.SplitWidget(!0,!1),t=new a.Widget.Widget;e.setMainWidget(t),e.show(this.contentElement),this.emptyWidget=new a.EmptyWidget.EmptyWidget(""),this.emptyWidget.show(t.element),this.workspaceDiff=c.WorkspaceDiff.workspaceDiff(),this.changesSidebar=new C(this.workspaceDiff),this.changesSidebar.addEventListener("SelectedUISourceCodeChanged",this.selectedUISourceCodeChanged,this),e.setSidebarWidget(this.changesSidebar),this.selectedUISourceCode=null,this.diffContainer=t.element.createChild("div","diff-container"),a.ARIAUtils.markAsTabpanel(this.diffContainer),this.diffContainer.addEventListener("click",(e=>this.click(e))),this.diffView=this.diffContainer.appendChild(new n.DiffView.DiffView),this.toolbar=new a.Toolbar.Toolbar("changes-toolbar",t.element);const i=new a.Toolbar.ToolbarButton(I(w.revertAllChangesToCurrentFile),"undo");i.addEventListener(a.Toolbar.ToolbarButton.Events.Click,this.revert.bind(this)),this.toolbar.appendToolbarItem(i),this.diffStats=new a.Toolbar.ToolbarText(""),this.toolbar.appendToolbarItem(this.diffStats),this.copyButton=new a.Toolbar.ToolbarButton(I(w.copyAllChangesFromCurrentFile),"copy",w.copy),this.copyButton.addEventListener(a.Toolbar.ToolbarButton.Events.Click,this.copyChanges.bind(this)),this.copyButtonSeparator=new a.Toolbar.ToolbarSeparator,this.toolbar.setEnabled(!1),this.hideDiff(I(w.noChanges)),this.selectedUISourceCodeChanged()}static instance(e={forceNew:null}){const{forceNew:t}=e;return T&&!t||(T=new D),T}selectedUISourceCodeChanged(){this.revealUISourceCode(this.changesSidebar.selectedUISourceCode()),this.selectedUISourceCode?.contentType()===e.ResourceType.resourceTypes.Stylesheet?(this.toolbar.appendToolbarItem(this.copyButtonSeparator),this.toolbar.appendToolbarItem(this.copyButton)):(this.toolbar.removeToolbarItem(this.copyButtonSeparator),this.toolbar.removeToolbarItem(this.copyButton))}revert(){const e=this.selectedUISourceCode;e&&this.workspaceDiff.revertToOriginal(e)}async copyChanges(){const e=this.selectedUISourceCode;if(!e)return;const i=await this.workspaceDiff.requestDiff(e,{shouldFormatDiff:!0});if(!i||i?.diff.length<2)return;const o=await s(i.diff);t.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(o)}click(t){if(this.selectedUISourceCode)for(const i of t.composedPath()){if(!(i instanceof HTMLElement))continue;if(i.ownerDocument.getSelection()?.toString())break;if(i.classList.contains("diff-line-content")&&i.hasAttribute("data-line-number")){let s=Number(i.dataset.lineNumber)-1;o.Runtime.experiments.isEnabled(o.Runtime.ExperimentName.PRECISE_CHANGES)&&this.#e&&(s=this.#e.formattedToOriginal(s,0)[0]),e.Revealer.reveal(this.selectedUISourceCode.uiLocation(s,0),!1),t.consume(!0);break}if(i.classList.contains("diff-listing"))break}}revealUISourceCode(e){this.selectedUISourceCode!==e&&(this.selectedUISourceCode&&this.workspaceDiff.unsubscribeFromDiffChange(this.selectedUISourceCode,this.refreshDiff,this),e&&this.isShowing()&&this.workspaceDiff.subscribeToDiffChange(e,this.refreshDiff,this),this.selectedUISourceCode=e,this.refreshDiff())}wasShown(){this.refreshDiff(),this.registerCSSFiles([u])}async refreshDiff(){if(!this.isShowing())return;if(!this.selectedUISourceCode)return void this.renderDiffRows();const e=this.selectedUISourceCode;if(!e.contentType().isTextType())return void this.hideDiff(I(w.binaryData));const t=await this.workspaceDiff.requestDiff(e,{shouldFormatDiff:o.Runtime.experiments.isEnabled(o.Runtime.ExperimentName.PRECISE_CHANGES)});this.selectedUISourceCode===e&&(this.#e=t?.formattedCurrentMapping,this.renderDiffRows(t?.diff))}hideDiff(e){this.diffStats.setText(""),this.toolbar.setEnabled(!1),this.diffContainer.style.display="none",this.emptyWidget.text=e,this.emptyWidget.showWidget()}renderDiffRows(e){if(!e||1===e.length&&e[0][0]===r.Diff.Operation.Equal)this.hideDiff(I(w.noChanges));else{this.diffStats.setText(function(e){const t=e.reduce(((e,t)=>e+(t[0]===r.Diff.Operation.Insert?t[1].length:0)),0),i=e.reduce(((e,t)=>e+(t[0]===r.Diff.Operation.Delete?t[1].length:0)),0),o=I(w.sDeletions,{n:i});return`${I(w.sInsertions,{n:t})}, ${o}`}(e)),this.toolbar.setEnabled(!0),this.emptyWidget.hideWidget();const t=this.selectedUISourceCode.mimeType();this.diffContainer.style.display="block",this.diffView.data={diff:e,mimeType:t}}}}class U{static instance(e={forceNew:!1}){const{forceNew:t}=e;return E&&!t||(E=new U),E}async reveal(e,t){if(!(e instanceof c.WorkspaceDiff.DiffUILocation))throw new Error("Internal error: not a diff ui location");await a.ViewManager.ViewManager.instance().showView("changes.changes"),D.instance().changesSidebar.selectUISourceCode(e.uiSourceCode,t)}}var k=Object.freeze({__proto__:null,ChangesView:D,DiffUILocationRevealer:U});export{v as ChangesSidebar,k as ChangesView};
